package com.xunmeng.chapter1;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

// 分散读
public class TestScatteringReads {
    public static void main(String[] args) {
        try (FileChannel channel = new RandomAccessFile(new File(System.getProperty("user.dir"), "word.txt"),"r").getChannel()) {

         // 创建三个buffer分别读取one two three
            ByteBuffer b1=ByteBuffer.allocate(3);
            ByteBuffer b2=ByteBuffer.allocate(3);
            ByteBuffer b3=ByteBuffer.allocate(5);
            // 读取
            channel.read(new ByteBuffer[]{b1,b2,b3});
            b1.flip();
            b2.flip();
            b3.flip();
            System.out.println(StandardCharsets.UTF_8.decode(b1));
            System.out.println(StandardCharsets.UTF_8.decode(b2));
            System.out.println(StandardCharsets.UTF_8.decode(b3));

        } catch (IOException e) {
        }
    }
}
